package com.csdn.boot;

import com.csdn.actuator.Actuator;
import com.csdn.actuator.ActuatorHolder;
import com.csdn.actuator.HttpActuator;
import com.csdn.server.ContainerServlet;
import com.sun.glass.ui.Application;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.servlet.Servlet;

@Component
public class BootStrap implements ApplicationRunner {
    public BootStrap(ActuatorHolder actuatorHolder) {
        this.actuatorHolder = actuatorHolder;
    }

    private final ActuatorHolder actuatorHolder;
    @Override
    public void run(ApplicationArguments args) throws Exception {
              //创建容器
        Server server = new Server(9003);
        ServletHandler handler = new ServletHandler();
        Actuator actuator = actuatorHolder.getActuator("HTTP");
        Servlet containerServlet = new ContainerServlet(actuator);
        ServletHolder servletHolder = new ServletHolder(containerServlet);
        handler.addServletWithMapping(servletHolder, "/*");
        //server.setHandler(handler);
        ServletContextHandler contextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        contextHandler.setServletHandler(handler);
        StatisticsHandler statisticsHandler = new StatisticsHandler();
        statisticsHandler.setHandler(server.getHandler());
        server.setHandler(statisticsHandler);
        server.setStopAtShutdown(false);
        server.start();
        server.join();
    }
}
